GBML manual / Lists

list_copy

Makes a new list that is a copy of source: the same items in the same order, and the same capacity (the most it can hold), whatever source holds right now. Changing the copy never changes source, and the other way round. It goes on the right-hand side of the line that makes the variable, like list_create, and it costs a whole second list of memory.

Syntax

list_copy(source) -> list<T>

Arguments

NameType
sourcelist

Returns

list<T>

Description

Makes a new list that is a copy of source: the same items in the same order, and room for the same number of items. Changing the copy never changes source, and changing source never changes the copy.

var backup = list_copy(source_list);

The copy is the same size

The copy has room for exactly as many items as source has room for - its capacity, which is not the same as how many it holds right now. A list made with list_create(40, 0) that holds 3 items makes a copy that holds the same 3 items and has room for 40, so you can keep adding to the copy. The capacity is the original's, and not the default of 8, so a copy is always big enough for everything the original can ever hold.

A copy takes its size from source, so there is no size for you to write. A copy given a smaller size of its own could not hold everything the original holds, so putting a size beside one (var backup: list<int, 4> = list_copy(source);, or a /// @capacity tag) is refused when the project is checked.

What it costs

A copy is a second whole list. The Game Boy Advance has no spare memory to hand out while a game runs, so the room for the copy is set aside when the game is built, in full, whether or not it ever fills up. A list with room for 200 items makes a copy that takes room for 200 items, even if it only holds 3 right now. Two copies take the room of two. A copy in an object's variable keeps its room for as long as the object exists, even when nothing uses it any more.

Making the copy takes time as well. It walks through every item that source holds and copies each one, so the time grows with how many there are. A few items copied when something happens - a button press, a new level - is nothing. Hundreds of items copied twice in every frame is a real part of the frame spent copying, and it adds up on a real cartridge. Copy when you need to, not every frame.

Where the room comes from depends on where the variable lives:

Where a copy can go

A copy is a new container, and its size belongs to the variable that holds it - so it goes in the same two places list_create does, and nowhere else:

Not inside another call or a sum, not on a line by itself, not in a later event such as Step, and not over a variable that already exists - that variable already has a size, and a copy of a different size cannot be put in it. To fill a list from another one later in the game, empty it with list_clear and add the items back one at a time in a loop.

What it can copy

source has to be a variable written on its own, like list_copy(scores), whose size the compiler knows: one made in the same event, or an object variable made in Create. Anything else is refused with a plain message:

Giving it the wrong kind of container is refused too: list_copy copies a list and nothing else.

list_length counts the items in the copy or in the original.

Example

var scores = list_create(8, 0);
list_add(scores, 10);
list_add(scores, 20);
var backup = list_copy(scores);   // holds 10 and 20, and has room for 8 like scores
list_add(backup, 30);             // scores still holds just 10 and 20

On the GBA

A copy of the fixed-size vector - the new one has the same N as source (its declared capacity, 8 if untagged), holds copies of the same items, and shares nothing with it. A list that holds 3 items with room for 200 makes a copy that takes 200 places of memory, not 3. A copy is a second whole container, so it costs a second container's worth of memory: as many places as the original has room for (its capacity, not just how many it holds right now), and copying is O(n) in how many items the original holds. The Game Boy Advance has no heap, so the copy's size is fixed when the game is built - it is the original's own declared size, which is why the original has to be a variable whose size is known here (otherwise it is refused: a script's parameter could be any size). Like a creation call it has to be the whole right-hand side of the line that makes a variable, and for an object's variable that line is in its Create event, where every instance of the object gets its own copy of the memory. A copy is always the same size as the original, so a size written beside it is refused.

See also: list_createlist_lengthListsarray_copystack_copyData containerslist_sortlist_shufflelist_filter